package cn.magicduck.demo.util;

import java.util.*;

/**
 * @projectName: design-pattern-demo
 * @className: TopicRandomUtil
 * @description: 无须描述
 * @author: MagicDuck
 * @create: 2020-07-21 14:02
 **/
public class TopicRandomUtil {
    /**
     * 乱序 Map 元素，记录对应答案 key
     * @param option 题目
     * @param key    答案
     * @return Topic 乱序后 {A=c., B=d., C=a., D=b.}
     */
    public static Topic random(Map<String, String> option, String key) {
        Set<String> keySet = option.keySet();
        ArrayList<String> keyList = new ArrayList<String>(keySet);
        Collections.shuffle(keyList);
        HashMap<String, String> optionNew = new HashMap<String, String>(option.size());
        int idx = 0;
        String keyNew = "";
        for (String next : keySet) {
            String randomKey = keyList.get(idx++);
            if (key.equals(next)) {
                keyNew = randomKey;
            }
            optionNew.put(randomKey, option.get(next));
        }
        return new Topic(optionNew, keyNew);
    }
}
